package com.ly.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.ly.pojo.vo.JoinActivityVO;
import com.ly.service.JoinActivityService;
import com.ly.utils.ExcludeToken;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author : 黑渊白花
 * @ClassName FileController
 * @date : 2025/4/2 10:57
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/fileUpload")
@Slf4j
public class FileController {
    @Value("${files.upload.path}")
    private String fileUploadPath;
    @Value("${files.upload.url}")
    private String host;
    @Autowired
    private JoinActivityService joinActivityService;

    @PostMapping("/upload")
    @ExcludeToken
    public Result upload(@RequestParam MultipartFile file) throws IOException {
        log.info("文件上传");
        //文件原始名称
        String originalFilename = file.getOriginalFilename();
        log.info("原来名称：{}",originalFilename);
        int index = originalFilename.lastIndexOf(".");
        //文件类型
        String extname = originalFilename.substring(index);
        //文件名
        // String newFileName = UUID.randomUUID().toString() + extname;
        String newFileName = System.currentTimeMillis() + extname;
        log.info("新名称：{}",newFileName);
        String newFilePath = fileUploadPath+newFileName;
        log.info("存储路径：{}",newFilePath);
        file.transferTo(new File(newFilePath));
        return Result.success(host+newFileName);
    }

    @GetMapping("/exportExcel")
    @ExcludeToken
    public void exportJoinActExcel(@RequestParam Integer actId, HttpServletResponse response) throws  IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        List<JoinActivityVO> all = joinActivityService.getAll(actId);
        JoinActivityVO joinActivityVO = all.get(0);
        String name = joinActivityVO.getActName() + "报名表";
        String fileName = URLEncoder.encode(name, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream())
                .head(JoinActivityVO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(fileName)
                .doWrite(all);
    }


    /**
     * 设置响应结果
     *
     * @param response    响应结果对象
     * @param rawFileName 文件名
     * @throws UnsupportedEncodingException 不支持编码异常
     */
    private void setExcelResponseProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }






}
